package b2.widget.scrollmsg
{
	import b2.net.*;
	import b2.util.*;
	
	import com.bit101.components.*;
	import com.smartfoxserver.v2.core.SFSEvent;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	
	public class ScrollMsg extends Sprite
	{
		public static const EVERY_DELETE_ITEM_COUNT:int = 10;
		
		public static const MAX_ITEM_COUNT:int = 100;

		//信息
		public var texts:Array;
		//下个行高
		public var nextTextHeight:int;
		
		public var container:Sprite;
		
		private var isAbleToClick:Boolean;
		//滚动窗体
		private var scrollPane:ScrollPane;
		 
		static private var instance:ScrollMsg;
		
		//发消息框
		private var sendMsgPanel:SendMsgPanel;

		public function ScrollMsg()
		{
			Style.setStyle(Style.LIGHT);
			//背景
			var rrect:Sprite = new Sprite();
			rrect.graphics.lineStyle(5, 0xFFFFFF);
			rrect.graphics.drawRoundRect(0, 0, 192, 162, 5, 5);
			rrect.filters = [new DropShadowFilter(1, 45, 0x000000, 1, 2, 2, 1, 1, false)];
			rrect.x = -2;
			rrect.y = -2;
			addChild(rrect);
			//滚动窗体
			scrollPane = new ScrollPane(this, 0, 0);
			scrollPane._hScrollbar.alpha = 0;
			scrollPane._background.alpha = 0.6; 
			scrollPane.width = 190;
			scrollPane.height = 138;
			texts = new Array();
			container = new Sprite();
			container.x = 5;
			container.y = 5;
			nextTextHeight = 0;
			scrollPane.content.addChild(container);
			sendMsgPanel = new SendMsgPanel(true);
			sendMsgPanel.y = 138;
			addChild(sendMsgPanel);
			this.y = BFont.stageHeight - 162;
			scrollPane.addEventListener(TextEvent.LINK, onClick);
			SmartFoxHelper.getInstance().sfs.addEventListener(SFSEvent.PUBLIC_MESSAGE, onMsgRecieved);
			SmartFoxHelper.getInstance().sfs.addEventListener(SFSEvent.PRIVATE_MESSAGE, onMsgRecieved);
			this.changeToHall();
		}
		
		static public function getInstance():ScrollMsg
		{
			if (!instance)
				instance = new ScrollMsg();
			return instance;
		}
		
		public function onMsgRecieved(event:SFSEvent):void
		{
			if (event.type == SFSEvent.PRIVATE_MESSAGE && event.params.sender.isItMe == true)
			{
				return;
			}
			addText(event.params["message"]);
			
			texts.push(event.params["message"]);
			
			//超过一百条
			if (texts.length > MAX_ITEM_COUNT)
			{
				for (var i:int = 0; i < EVERY_DELETE_ITEM_COUNT; i++)
				{
					texts.shift();
				}
				refresh();
			}
		}
		
		//加一条
		public function addText(msg:String):void
		{
			var t:TextField = new TextField();
			t.y = nextTextHeight;
			t.wordWrap = true;
			t.width = 175;
			t.defaultTextFormat = new TextFormat("微软雅黑", 12, 0x000000);
			t.autoSize = TextFieldAutoSize.LEFT;
			var senconds:int = ServerHelper.getInstance().currentSeconds();
			var minutes:int = int(senconds / 60) % 60;
			var honours:int = int(senconds / 3600) % 24;
			var timeStr:String = (honours < 10 ? "0" : "") + honours + ":" + (minutes < 10 ? "0" : "") + minutes;
			t.htmlText = "<font color='#00AAAA'>" + timeStr + "</font> " + msg;
			scrollPane._vScrollbar.maximum = nextTextHeight;
			nextTextHeight += t.textHeight;
			container.addChild(t);
			//滚到最下面
			scrollPane._vScrollbar.value = scrollPane._vScrollbar.maximum - scrollPane.height + 40;
			scrollPane._hScrollbar.alpha = 0;
			scrollPane.update();
			//样式调整
			Style.BUTTON_FACE = 0xffffff;
		}
		
		public function onClick(event:TextEvent):void
		{
			if (!isAbleToClick)
				return;
			//事件;参数
			var strs:Array = event.text.split(";");
			if (strs[0] == "joinRoom")
				SmartFoxHelper.getInstance().joinRoom(strs[1]);
		}
		
		public function refresh():void
		{
			scrollPane.content.removeChild(container);
			container = new Sprite();
			container.x = 5;
			container.y = 5;
			scrollPane.content.addChild(container);
			nextTextHeight = 0;
			for (var i:int = 0, len:int = texts.length; i < len; i++)
			{
				addText(texts[i]);
			}
		}
		
		public function changeToRoom():void
		{
			var tempParent:DisplayObjectContainer = this.parent;
			if (tempParent != null)
			{
				tempParent.removeChild(this);
				tempParent.addChild(this);
			}
			isAbleToClick = false;
			sendMsgPanel.setNeedLoudSpeaker(false);
		}
		
		public function changeToHall():void
		{
			isAbleToClick = true;
			sendMsgPanel.setNeedLoudSpeaker(true);
		}
	}
}